package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<BrandDTO> queryBrandByCategory(Long categoryId) {
        // 根据分类查询
        List<Brand> list =  getBaseMapper().queryByCategoryId(categoryId);
        // 转换DTO并返回
        return BrandDTO.convertEntityList(list);
    }

    @Override
    public PageDTO<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows) {
        if(page <= 0 || rows <= 0){
            throw new LyException(400, "页码超出范围！");
        }
        // 1.判断是否有搜索条件
        boolean isNotBlank = StringUtils.isNotBlank(key);
        // 2.查询 SELECT b.* // FROM tb_brand b
        Page<Brand> result = query()
                // WHERE b.`name` LIKE "%X%" OR b.letter = "X"
                .like(isNotBlank, "`name`", key).or().eq(isNotBlank, "letter", key)
                // LIMIT 0, 5
                .page(new Page<>(page, rows));
        // 3.封装结果
        List<Brand> brandList = result.getRecords();
        List<BrandDTO> dtoList = BrandDTO.convertEntityList(brandList);
        // 返回
        return new PageDTO<>(result.getTotal(), result.getPages(), dtoList);
    }

    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO) {
        // 1.新增品牌
        // 1.1.转PO
        Brand brand = brandDTO.toEntity(Brand.class);
        // 1.2.新增
        boolean isSuccess = save(brand);
        if(!isSuccess){
            throw new LyException(500, "新增品牌失败！");
        }
        // 1.3.回显id
        Long brandId = brand.getId();

        // 2.新增品牌的中间表
        // 2.1.获取分类id
        List<Long> categoryIds = brandDTO.getCategoryIds();
        // 2.2.封装中间表对象
        List<CategoryBrand> cbList = categoryIds.stream()
                .map(categoryId -> CategoryBrand.of(categoryId, brandId))
                .collect(Collectors.toList());
        /*List<CategoryBrand> cbList = new ArrayList<>(categoryIds.size());
        for (Long categoryId : categoryIds) {
            cbList.add(CategoryBrand.of(categoryId, brandId));
        }*/
        // 2.3.批量新增中间表
        categoryBrandService.saveBatch(cbList);
    }
}